package com.ymatou.iapi.productmanager.testcase;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productmanager.service.ModifyActivityProductSortV3Call;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.LiveCallService;
import com.ymttest.business.service.NewJavaMongoCheckService;
import com.ymttest.business.service.productdata.impl.ProductFactory;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.annotation.product.IntergratedProductMapper;
import com.ymttest.database.mongo.YmtMongoDriverFactroy;
import com.ymttest.utils.EnvSetup;

public class Ts_ModifyActivityProductSortV3 {
	private static String sellerName = EnvSetup.getData("productsellername");
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));
	private static ModifyActivityProductSortV3Call modifyActivityProductSortV3Call= new ModifyActivityProductSortV3Call();
	private static IntergratedProductMapper intergratedProductMapper = SqlMapperFactory.getIntergratedProductMapper();
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("直播商品排序");
	}
	
	@Before
	public void caseUp() {
	}
	
	@AfterClass
	public static void tearDown() {
		Logger.generateResult(modifyActivityProductSortV3Call.getOpurl());
	}
	
	@After	
	public void caseDown() {
		Logger.end();
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ModifyActivityProductSortV3_001() throws Exception {
		Logger.start(true,"直播商品全排序");
		try{
			JSONObject liveResponseBean = LiveCallService.createLiveInProgress(sellerName, sellerId);
			int liveId =  liveResponseBean.getJSONObject("Data").getInt("ActivityId");
			
			List<String> productIds = ProductFactory.getProductWithExistStartLive(sellerId, sellerName, liveId, 5)
													.stream().map(x->x.getProductId()).collect(Collectors.toList());
			//乱序后全排序
			Collections.shuffle(productIds);
			
			JSONObject ret = modifyActivityProductSortV3Call.call(sellerId, liveId, productIds);
			
			checkModifyActivityProductSortV3(ret,liveId,productIds);
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ModifyActivityProductSortV3_002() throws Exception {
		Logger.start(true,"非该直播sellerId，直播商品全排序");
		try{
			JSONObject liveResponseBean = LiveCallService.createLiveInProgress(sellerName, sellerId);
			int liveId =  liveResponseBean.getJSONObject("Data").getInt("ActivityId");
			
			List<String> productIds = ProductFactory.getProductWithExistStartLive(sellerId, sellerName, liveId, 5)
													.stream().map(x->x.getProductId()).collect(Collectors.toList());
			//乱序后全排序
			Collections.shuffle(productIds);
			
			
			List<String> actProdSortList = intergratedProductMapper.selectLiveProdLiveId(liveId).stream()
					.sorted(Comparator.comparing(x->new BigDecimal(x.get("dReadyPutawayProductSort").toString()).intValue()))
					.map(y->y.get("sProductId").toString())
					.collect(Collectors.toList());
			
			JSONObject ret = modifyActivityProductSortV3Call.call(sellerId+1, liveId, productIds);
			
			checkModifyActivityProductSortV3(ret,liveId,actProdSortList);
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	public static void checkModifyActivityProductSortV3(JSONObject ret,int liveId, List<String> tgtProdSortList) throws Exception{
		Logger.verifyEquals(200, ret.getInt("Code"), "返回码检查");
		
		List<String> actProdSortList = intergratedProductMapper.selectLiveProdLiveId(liveId).stream()
								.sorted(Comparator.comparing(x->new BigDecimal(x.get("dReadyPutawayProductSort").toString()).intValue()))
								.map(y->y.get("sProductId").toString())
								.collect(Collectors.toList());
		
		System.out.println(tgtProdSortList);
		System.out.println("=================");
		System.out.println(actProdSortList);
		
		for (int i=0;i<actProdSortList.size();i++){
			Logger.verifyEquals(tgtProdSortList.get(i), actProdSortList.get(i), "排序结果应该相同");
		}
		
		
		//mongo check
		if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
			NewJavaMongoCheckService.checkLiveMgoSyncBySQL_NewJavaMgo(liveId);
		}
	}
	
	
	public static List<String> getAllProdNewSortList(List<String> prodOldAllSort,List<String> reqSort){
		  for (int i = 0; i < reqSort.size(); i++) {
	            String sortPid = reqSort.get(i);
	            if (prodOldAllSort.remove(sortPid)) {
	            	prodOldAllSort.add(i, sortPid);
	        }
		
		  }
		  return prodOldAllSort;
	}
}